package vashion.azeroth.core.item.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import vashion.azeroth.core.pojo.BaseObject;

import java.util.Date;
import java.util.Map;

/**
 * 属性值对象
 * Created by liuhe on 15/9/13.
 */
@Data
public class PropertyValueDO extends BaseObject {

    private static final long serialVersionUID = 1L;

    private final String featureImage = "image";

    /**
     * 主键
     */
    private long id;

    /**
     * 无线端适配Id
     */
    private long iosId;

    /**
     * 创建时间
     */
    private Date gmtCreate;

    /**
     * 修改时间
     */
    private Date gmtModified;

    /**
     * 属性元Id
     */
    private long propertyMetaId;

    /**
     * 属性元title，冗余字段
     */
    private String propertyMetaTitle;

    /**
     * 属性状态
     */
    private Integer status;

    /**
     * 是否可以追加图片
     */
    private int couldAppendImage;

    /**
     * 属性值
     */
    private String value;

    /**
     * 属性附加信息
     */
    private String feature;

    private String skuImage;
	
	/**
	 * 库存，方便前台操作
	 */
	private int stock;
    
    /**
     * 商品颜色编码和名字
     **/
    private String color;

    public long getIosId() {
        return id;
    }

    public void setSkuImage(ItemSkuDO itemSkuDO) {
        this.skuImage = itemSkuDO.getPic();
    }

    public String getFeatureImage() {
        if (StringUtils.isBlank(this.feature)) {
            return "";
        }
        Map<String, String> featureMap = this.getFeatureMap();
        return featureMap.containsKey(featureImage) ? featureMap.get(featureImage) : "";
    }

    private Map<String, String> getFeatureMap() {
        if (StringUtils.isBlank(this.feature)) {
            return Maps.newHashMap();
        }
        return JSON.parseObject(this.feature, new TypeReference<Map<String, String>>() {
        });
    }

}
